package com.xl.rentkeeper.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.xl.rentkeeper.R;


public class FadingScrollView extends ScrollView {
    private static String TAG = "FadingScrollView";

    private View mActionBar;
    private Drawable mBgDrawable;

    private ImageView fadingBar;

    private int fadingHeight;   //可隐藏的控件高度
    private int oldY;
    private int fadingOffset;

    public static final int ALPHA_START=0;
    public static final int ALPHA_END=255;

    public FadingScrollView(Context context) {
        this(context, null);
    }

    public FadingScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FadingScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        fadingBar = (ImageView) findViewById(R.id.iv_activity_bg);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        fadingHeight = fadingBar.getMeasuredHeight()-fadingOffset;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                oldY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int scrollY = getScrollY();

                Log.i(TAG, "scrollY:" + scrollY + " ;-fadingHeight" + fadingHeight);
                int y = (int) ev.getY();
                int deltaY = y - oldY;

                int willScrollY = scrollY - deltaY;

//                if (willScrollY > fadingHeight) {
//                    willScrollY = fadingHeight;
//                }

                if (willScrollY < 0) {
                    willScrollY = 0;
                }

                scrollTo(0, willScrollY);
                int tmpfadingHeight =0;
                if (willScrollY > fadingHeight) {
                    tmpfadingHeight = willScrollY;
                }else{
                    tmpfadingHeight= fadingHeight;
                }
                updateActionBarAlpha(willScrollY*(ALPHA_END-ALPHA_START)/tmpfadingHeight+ALPHA_START);
                oldY = y;

                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return true;
    }

    public void bindingActionBar(View actionBar) {
        mActionBar = actionBar;
    }


    public void setActionBarAlpha(int alpha) throws Exception{
        if(mActionBar==null){
            throw new Exception("acitonBar is not binding or bgDrawable is not set.");
        }
        mActionBar.getBackground().setAlpha(alpha);
    }

    void updateActionBarAlpha(int alpha){
        try {
            setActionBarAlpha(alpha);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setFadingOffset(int height){
        fadingOffset=height;
    }
}